import { Model } from 'mongoose';
import { HttpErrorException } from 'src/common/exceptions';
import { successAdd } from 'src/common/responses';
import { ResponseBodyBaseType } from 'src/typings';

/**
 *  向数据库文档添加新的数据
 * @param model MongoDBModel
 * @param doc 要创建数据的MongoDB的Doc
 * @returns 创建结果
 */
export const addOne = async <M>(
  model: Model<M>,
  doc: any,
) => {
  try {
    const res = await new model(doc).save();
    console.log('res', res.toObject());

    return successAdd();
  } catch (error: any) {
    throw new HttpErrorException(new Error(error).message);
  }
};
